package com.kedou.sdk.ui.assist;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.kedou.sdk.core.HttpEngineApi;
import com.kedou.sdk.core.HttpEngineConduits;
import com.kedou.sdk.params.bean.SimpleUserInfo;
import com.kedou.sdk.utils.ResUtils;

import java.util.ArrayList;
import java.util.List;

public class AccountTokenListAdapter extends BaseAdapter {

    private final List<SimpleUserInfo> mChannels = new ArrayList<>();
    private LayoutInflater inflater;
    private boolean isFirstLoadView;
//    private int lastSelectedIndex=-1;

    public void setAdapterData(List<SimpleUserInfo> data) {
        this.mChannels.clear();
        this.mChannels.addAll(data);
        this.isFirstLoadView = true;
    }
    HttpEngineConduits wanSDKDataDao = HttpEngineApi.getInstance().getEngineConduits();
//    public void setLastSelectedIndex(int position) {
//        this.lastSelectedIndex = position;
//    }

    @Override
    public int getCount() {
        return mChannels.size();
    }

    @Override
    public SimpleUserInfo getItem(int position) {
        return mChannels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Context context = parent.getContext();
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();

            if (inflater == null) {
                inflater = LayoutInflater.from(context);
            }

            final int layoutResId = ResUtils.getLayoutIdentifier(context, "hnssn_item_account_token");
//            final int iconResId = ResUtils.getViewIdentifier(context, "hnssn_item_account_icon");
            final int titleResId = ResUtils.getViewIdentifier(context, "hnssn_item_account_name");
            final int checkResId = ResUtils.getViewIdentifier(context, "hnssn_item_account_checkbox");
            final int lastLogin = ResUtils.getViewIdentifier(context, "hnssn_item_account_lastLogin");
            final int moren = ResUtils.getViewIdentifier(context, "hnssn_item_account_moren");

            convertView = inflater.inflate(layoutResId, parent, false);
            viewHolder.account = convertView.findViewById(titleResId);
//            viewHolder.icon = convertView.findViewById(iconResId);
            viewHolder.checked = convertView.findViewById(checkResId);
            viewHolder.lastLogin = convertView.findViewById(lastLogin);
            viewHolder.defaultText = convertView.findViewById(moren);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        SimpleUserInfo channel = getItem(position);
        if (channel != null) {
            viewHolder.account.setText(TextUtils.isEmpty(channel.getNickname())?channel.getUsername():channel.getNickname());
        }
        viewHolder.checked.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Log.e("adapterchange","po"+position+"b"+b);
//                lastSelectedIndex = position;
                if(b){
                    wanSDKDataDao.saveDefaultSmallAccount(getItem(position).getId());
                    notifyDataSetChanged();
                }

            }
        });
        viewHolder.checked.setChecked(getItem(position).getId().equals(wanSDKDataDao.readDefaultSmallAccount()));
        viewHolder.defaultText.setBackgroundResource(getItem(position).getId().equals(wanSDKDataDao.readDefaultSmallAccount())?
                ResUtils.getDrawableIdentifier(context,"lysysdk_account_default_select"):
                ResUtils.getDrawableIdentifier(context,"lysysdk_account_default_noselect"));
        viewHolder.defaultText.setTextColor(getItem(position).getId().equals(wanSDKDataDao.readDefaultSmallAccount())?
                context.getColor(ResUtils.getColorIdentifier(context,"wan669_f8FC31F")):
                context.getColor(ResUtils.getColorIdentifier(context,"wan669_f7c7b7b_50")));
        viewHolder.lastLogin.setVisibility(getItem(position).getId().equals(wanSDKDataDao.readLastLoginSmallAccount())?View.VISIBLE:View.GONE);
        if (isFirstLoadView) {
            isFirstLoadView = false;
        }

        return convertView;
    }

    public static class ViewHolder {
        public TextView account;
        public TextView lastLogin;
        public TextView defaultText;
//        public ImageView icon;
        public CheckBox checked;
    }

}
